﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkitDemo.ObserverModel;
using DataBaseLib.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;

namespace CommunityToolkitDemo.ViewModel
{
    public class MainViewModel:ObservableObject
    {
        #region 属性
        [ObservableProperty]
        private string dateTimeStr;
        public string DateTimeStr
        {
            get { return dateTimeStr; }
            set
            {
                SetProperty(ref dateTimeStr, value);
            }
        }

        private ObservableCollection<ObservableUser> userList;
        public ObservableCollection<ObservableUser> UserList { get => userList; set => SetProperty(ref userList, value); }

        private ObservableUser currentUser;
        public ObservableUser CurrentUser { get => currentUser; set => SetProperty(ref currentUser, value); }

        private ObservableCollection<UserInfo> userInfoList;
        public ObservableCollection<UserInfo> UserInfoList { get => userInfoList; set => SetProperty(ref userInfoList, value); }

        private UserInfo currentUserInfo;
        public UserInfo CurrentUserInfo { get => currentUserInfo; set => SetProperty(ref currentUserInfo, value); }

        #endregion

        #region 命令

        public RelayCommand<string> ButtonClickCmd {  get; set; }


        #endregion

        public MainViewModel()
        {
            ButtonClickCmd = new RelayCommand<string>(FunMenu);

            //创建定时器
            CreateTimer();

            InitUserList();

        }

        private void InitUserList()
        {
            //假设这些数据来自数据库
            var dbUserList = GetDemoData();

            UserList = new ObservableCollection<ObservableUser>(dbUserList.Select(x => new ObservableUser(x)));

            UserInfoList= new ObservableCollection<UserInfo>(dbUserList);
        }
        private void InitUserListChange()
        {
            UserInfo user = new UserInfo();
            user.Id = 3;
            user.Name = "王五";
            UserInfoList.Add(user);
            UserList.Add(new ObservableUser(user));
        }
        private List<UserInfo> GetDemoData()
        {
            var list = new List<UserInfo>();
            UserInfo user1 = new UserInfo() { Id = 1, Name = "张三" };
            UserInfo user2 = new UserInfo() { Id = 2, Name = "李四" };
            list.Add(user1);
            list.Add(user2);
            return list;
        }
      
        public void FunMenu(string p)
        {
            switch (p)
            {
                case "最小化":
                    WindowMin();
                    break;
                case "最大化":
                    WindowMax();
                    break;
                case "关闭":
                    AppClose();
                    break;
                case "改变数值":
                    ChangeValue();
                    break;
            }
        }

        private void ChangeValue()
        {
            InitUserListChange();
        }

        private void AppClose()
        {
            WeakReferenceMessenger.Default.Send("MainWindowClose");
        }

        private void WindowMax()
        {
            WeakReferenceMessenger.Default.Send("MainWindowMax");
        }

        private void WindowMin()
        {
            WeakReferenceMessenger.Default.Send("MainWindowMin");
        }

        private void CreateTimer()
        {
            #region 系统时间定时器获取时间数据
            DispatcherTimer cpuTimer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 1, 0)
            };
            cpuTimer.Tick += DispatcherTimer_Tick;
            cpuTimer.Start();
            #endregion
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            DateTimeStr = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }
    }
}
